package com.dwb.service;

import com.dwb.config.BizException;
import com.dwb.entity.SGoodsCategory;
import com.dwb.mappers.SGoodsCategoryMapper;
import com.dwb.result.ResultCode;
import jdk.nashorn.internal.ir.LiteralNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

import static com.dwb.common.check.Check.*;

/**
 * @Author manke
 * @Date 2020/6/24 16:23
 * @Version 0.0.1
 */
@Service
public class SGoodsCategoryService {
    @Autowired
    private SGoodsCategoryMapper sGoodsCategoryMapper;

    public void insert(SGoodsCategory sGoodsCategory) {
        CHECK_GOODS_CATEGORY_INSET_OR_UPDATE(sGoodsCategory);

        sGoodsCategoryMapper.insert(sGoodsCategory);


    }
    public List<SGoodsCategory> getList() {
        return sGoodsCategoryMapper.selectAll();


    }

    public SGoodsCategory getInfo(Map<String, String> parameters) {
        CHECK_GOODS_CATEGORY_GET_INFO(parameters);
        return sGoodsCategoryMapper.selectByPrimaryKey(parameters.get("id"));
    }

    public void update(SGoodsCategory sGoodsCategory) {
        CHECK_GOODS_CATEGORY_INSET_OR_UPDATE(sGoodsCategory);
        sGoodsCategoryMapper.updateByPrimaryKeySelective(sGoodsCategory);
    }

    public void delete(Map<String, String> parameters) {
        CHECK_GOODS_CATEGORY_DELETE(parameters);
        Example example = new Example(SGoodsCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",parameters.get("id"));
        List<SGoodsCategory> sGoodsCategories = sGoodsCategoryMapper.selectByExample(example);
        if (sGoodsCategories.size()>0){
            throw new BizException(ResultCode.CATEGORY_NOT_DELETE);
        }
        sGoodsCategoryMapper.deleteByPrimaryKey(parameters.get("id"));
    }
}
